/*1942 game clone
 * Authors:
 * Abdollah Tabareh
 * Bharat Bhushan Konka
 * Martin Svensson 
 * Richard Zianne
 * Tigran Harutyunyan
 * 
 * 2010
*/


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Game1942.Handelers
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Wave2Handeler : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private Texture2D _texture;
        private Game _game;
        private int _counter = 0;
        private Vector2 _waveDirection;
        private int _speed = 2;
        private int _waveCounter = 5;
        private TimeSpan _startTime = TimeSpan.Zero;
        private bool _startWave = false;


        private GameScene _gameScene;

        // List of active planes
        protected List<Enemy2> _planes;
        public Wave2Handeler(Game game, ref Texture2D texture) : base(game)
        {
            _texture = texture;
            _planes = new List<Enemy2>();
            _game = game;
            _waveDirection = Direction.Down;
        }

        internal void Initialize(GameScene gameScene)
        {
            _gameScene = gameScene;
            Initialize();
        }
        
        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            //_planes.Clear;
            for (int i = 0; i < 9; i++)
            {
                AddPlane(i);
            }

            base.Initialize();
        }

        private void AddPlane(int planeNumber)
        {
            Enemy2 newEnemy = new Enemy2(_game, ref _texture);
            newEnemy._index = planeNumber;
            newEnemy.putInPosition(calculatePosition(planeNumber, newEnemy));
            
            

            //newEnemy.putInPosition((float)80 * planeNumber, 0);
            newEnemy._direction = Direction.Down;
            newEnemy._planeSpeed = _speed;
            _planes.Add(newEnemy);


        }

        private Vector2 calculatePosition(int planeNumber, Enemy2 enemy)
        {
            planeNumber++;
            Vector2 value = new Vector2();
            if (planeNumber % 2 == 0)//place to right
            {
                value.X = (Util.ScreenWidth / 2) + (10 * planeNumber);
                value.Y -= enemy._planeHight * planeNumber;
            }
            else //plaseLeft
            {
                value.X = (Util.ScreenWidth / 2) - (10 * planeNumber) - enemy._planeWith;
                value.Y -= (enemy._planeHight * planeNumber) + enemy._planeWith;
            }
            return value;
        }

        



        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (!_startWave)
            {
                if (_startTime.Equals(TimeSpan.Zero))
                {
                    _startTime = gameTime.TotalRealTime;
                    return;
                }
                if (gameTime.TotalRealTime.Subtract(_startTime).Seconds < 20)
                {                    
                    return;
                }
                _startWave = true;
            }
            // Update Planes
            for (int i = 0; i < _planes.Count; i++)
            {
                if (_planes[i]._isDead)
                {
                    _planes.RemoveAt(i);
                    
                }
            }
            loop();
            //loopLeft();

           
            
                  
                   
            for (int i = 0; i < _planes.Count; i++)
            {
                _planes[i]._index = i;
                if (_planes[i].getPosition().Y == Util.ScreenHeight)
                {
                    _planes[i].putInPosition(calculatePosition(_planes[i]._index,_planes[i]));
                }
                //Fire a shot
                if ( (Util.Instance.RandomGenerator.Next(300) == 1))
                {
                   _gameScene.fireBullit(_planes[i]._direction,_planes[i]._position,false);

                }

                
                _planes[i]._direction = _waveDirection;
                _planes[i]._planeSpeed = _speed;
                _planes[i].Update(gameTime);
                
                
            }
            
            base.Update(gameTime);
        }



        private void loop()
        {
            if (_counter > Util.ScreenHeight + 300)
            {
                _waveCounter--;
                _counter = 0;

            }
                _waveDirection = Direction.Down;
                _counter++;
        }

        /// <summary>
        /// Allows the game component draw your content in game screen
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            /*if (_waveCounter == 0)
            {
                return;
            }*/
            
            for (int i = 0; i < _planes.Count; i++)
            {
                _planes[i].Draw(gameTime);
            }

            base.Draw(gameTime);
        }
        public void CheckCollision()
        {

        }

        public Enemy2[] getEnemys()
        {
            return _planes.ToArray();
        }
        
    }
}